package com.hundun.hddeal.controller;

import com.hundun.hddeal.dao.GzhMapper;
import com.hundun.hddeal.entity.AdUser;
import com.hundun.hddeal.entity.Gzh;
import com.hundun.hddeal.response.Result;
import com.hundun.hddeal.response.ResultUtil;
import com.hundun.hddeal.service.lmpl.AdUserServiceImpl;
import com.hundun.hddeal.service.lmpl.MtUserServiceImpl;
import com.hundun.hddeal.util.RedisUtil;
import com.hundun.hddeal.util.TokenUtilT;
import com.hundun.hddeal.vo.GzhVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;


/**
 * redis缓存
 * @author 谢
 */
@Slf4j
@RestController
@RequestMapping("buyCar")
public class RedisController {

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    GzhMapper gzhMapper;
    @Autowired
    MtUserServiceImpl mtUserService;
    @Autowired
    AdUserServiceImpl adUserService;

    /**
     * 添加购物车
     * @return
     */
    @PostMapping("addCar")
    public Result addCar(@RequestBody GzhVo gzhVo){
        String adUserId = TokenUtilT.getTokenById(gzhVo.getAdUserToken());
        AdUser adUser = adUserService.findAdUserById(adUserId);
        if(adUser==null){
            return ResultUtil.error("无token");
        }
        List <Gzh> gzhList= gzhMapper.selectByGhIds(gzhVo.getGhIds());
        List<Gzh> list= (List<Gzh>) redisUtil.get(adUser.getAdId());
        if(list!=null){
            for (Gzh gzh:gzhList) {
                 list.add(gzh);
            }
            for  ( int  i  =   0 ; i  <  list.size()  -   1 ; i ++ )  {
                for  ( int  j  =  list.size()  -   1 ; j  >  i; j -- )  {
                    if  (list.get(j).equals(list.get(i)))  {
                        list.remove(j);
                    }
                }
            }
            //adId作为key，存入redis
            if(redisUtil.set(adUser.getAdId(),list)){
                return  ResultUtil.success();
            }
        }else {
            //adId作为key，存入redis
            if(redisUtil.set(adUser.getAdId(),gzhList)){
                return  ResultUtil.success();
            }
        }


       return ResultUtil.error();
    }


    /**
     * 获取媒体用户购物车列表
     * @param gzhVo
     * @return
     */
     @PostMapping("getCar")
     public Result getCar(@RequestBody GzhVo gzhVo){
         String adUserId = TokenUtilT.getTokenById(gzhVo.getAdUserToken());
         AdUser adUser = adUserService.findAdUserById(adUserId);
         if(adUser!=null){
            return ResultUtil.success(redisUtil.get(adUser.getAdId()));
         }
         return ResultUtil.error("无token");
     }


    /**
     * 删除购物车
     * @param gzhVo
     * @return
     */
     @PostMapping("delCar")
     public Result delCar(@RequestBody GzhVo gzhVo){
         String adUserId = TokenUtilT.getTokenById(gzhVo.getAdUserToken());
         AdUser adUser = adUserService.findAdUserById(adUserId);
        if(adUser!=null){
        List<Gzh> list= (List<Gzh>) redisUtil.get(adUser.getAdId());
        List <Gzh> gzhList= gzhMapper.selectByGhIds(gzhVo.getGhIds());
        for (Gzh gzh:gzhList) {
            for(int i=0;i<list.size();i++){
                //如果ghid相同则删除
                if(gzh.getGhId().equals(list.get(i).getGhId())){
                    list.remove(list.get(i));
                }
            }
        }
        if(redisUtil.set(adUser.getAdId(),list)){
            return ResultUtil.success("删除成功");
        }

         }
         return ResultUtil.error("无token");
     }







}
